import { Component, OnInit, Input } from '@angular/core';
import { MessageService } from 'src/app/shared/service/message.service';
import { HttpService } from 'src/app/shared/service/http.service';

import { AppGlobalService } from './../../shared/service/app-global.service';
import { Subscription } from 'rxjs';

@Component({
    selector: 'app-investment-list',
    templateUrl: './investment-list.component.html',
    styleUrls: ['./investment-list.component.scss'],
})
export class InvestmentListComponent implements OnInit {
    @Input() tagData: any; // 标签数据
    listdata: any;
    // 筛选及页面数据参数
    params: any = {};

    // 当前页数，默认1
    pageNo = 1;

    // size
    pageSize: number;

    // 数量
    pageTotal: number;
    // 声明订阅对象
    subscription: Subscription = null;

    refresherId = 'refreshFiledId';


    constructor(
        private appGlobalService: AppGlobalService,
        private message: MessageService,
        private http: HttpService
    ) { }

    ngOnInit() {
        this.refresherId += new Date().getTime();
        // 初始化参数
        this.params = this.tagData.data.submit;
        this.params.sessionId = localStorage.getItem('sessionId');
        this.pageSize = this.tagData.pageSize;
        this.pageTotal = this.tagData.pageTotal;
        this.initListData();
        // 订阅未注销的 不重复注册 节省资源
        if (this.appGlobalService.globalSubscription && this.appGlobalService.globalSubscription.closed === false) {
            return;
        }
        if (!this.subscription && this.tagData.data.submit !== undefined) {
            this.message.clearMessage();
            // 获取筛选参数
            this.subscription = this.message.getMessage().subscribe(param => {
                if (param === undefined) {
                    return;
                } else if (param.type === 'ionRefresh') {
                    // this.refreshData(param);
                    return;
                }
                this.pageNo = 1;
                this.params.sessionId = localStorage.getItem("sessionId");
                // 参数
                this.params.content = param;
                // 页面数据参数
                this.params.content.pageNo = this.pageNo;
                // 请求获取新数据
                this.http.post(
                    "application/submit",
                    this.params,
                    res => {
                        console.log("submit");
                        // 请求数据成功
                        if (res.success) {
                            // 切换submit返回数据
                            if (Array.isArray(res.data.data) && res.data.data.length > 0) {
                                // // 追加数据
                                this.tagData.data.data = res.data.data;
                                this.params = res.data.submit;
                                this.initListData();
                            } else {
                                this.tagData.data.data = res.data.data;
                            }
                        } else {
                            // 请求数据失败
                            this.appGlobalService.showToast(res.message, 2000, "middle");
                        }
                    },
                    err => {
                        // 请求数据失败
                        this.appGlobalService.showToast(err.message, 2000, "middle");
                    }
                );
            });
        }
    }

    initListData() {
        this.listdata = this.tagData.data.data;
        this.listdata.forEach(element => {
            element.imgdata = [];
            if (element.contentView.icons) {
                if (element.contentView.icons.iconsUrl) {
                    const imgdataitem = element.contentView.icons.iconsUrl.split(",");
                    imgdataitem.map(v => {
                        if (v) {
                            element.imgdata.push(v);
                        }
                    });
                }
            }
        });
    }


    loadMoreData(event) {
        if (this.pageNo >= this.pageTotal) {
            //   this.appGlobalService.showToast('没有数据了...', 1000, 'middle');
            // event.complete();
            event.target.disable = true;
            return;
        }
        // // 请求下一个页面
        this.pageNo = this.pageNo + 1;
        // // 参数
        this.params.content.pageNo = this.pageNo;
        // 请求获取新数据
        this.http.post('application/submit', this.params, res => {

            // 请求数据成功
            if (res.success) {
                const resList = res.data.data;
                // 追加返回数据
                if (resList && resList.length > 0) {
                    // 追加数据
                    resList.map(item => {
                        this.listdata.push(item);
                    });
                } else {
                    // 没有数据了
                    // this.appGlobalService.showToast('没有数据了...', 1000, 'middle');
                    event.target.disable = true;
                }
            } else {
                // 请求失败，更新当前页数
                this.pageNo = this.pageNo - 1;
                // 请求数据失败
                this.appGlobalService.showToast(res.message, 2000, 'middle');
            }
            event.target.complete();

        }, err => {
            // 请求失败，更新当前页数
            this.pageNo = this.pageNo - 1;
            // 请求数据失败
            this.appGlobalService.showToast(err.message, 2000, 'middle');
            event.target.disable = true;

        });

    }
    // 跳转
    go(obj) {
        if (!obj) {
            console.log('没有返回link-target');
        }
        const pageParams = {
            pageId: obj.target,
            params: obj.params,
            sessionId: localStorage.getItem('sessionId')
        };
        this.appGlobalService.goToForward(pageParams);
    }

    doRefresh(event) {
        document.getElementById(this.refresherId).style.marginTop = "65px";
        setTimeout(() => {
            this.params.sessionId = localStorage.getItem("sessionId");
            this.params.content.pageNo = 1;
            this.http.post('application/submit', this.params, res => {
                if (res.success) {
                    this.tagData.data.data = res.data.data;
                    this.initListData();
                    this.params.content.pageNo = 2;
                    event.target.complete();
                    document.getElementById(this.refresherId).style.marginTop = "0px";
                } else {
                    this.pageNo = 1;
                    this.appGlobalService.showToast(res.message, 2000, 'middle');
                    event.target.complete();
                    document.getElementById(this.refresherId).style.marginTop = "0px";
                }
            });
        }, 300);
    }
}
